﻿@using KPIS.GERP.Models.SYS.Resources
@model KPIS.GERP.Models.ICM.BidderQuotation

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "เพิ่ม เสนอราคา";
    ViewBag.Descr = "เพิ่ม เสนอราคา";
}

@using (Html.BeginForm("Create", "BidderQuotation", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <div class="block-button-menu-above">
        @Html.ActionLink("กลับไปหน้าแสดงข้อมูลทั้งหมด", "Index", new { BidderId = Model.BidderId }, new { @class = "btn btn-gray button-standard-width btn-small", role="button" })
        @*@Html.ButtonViewAll(false, "Index", .ToString())*@
        @Html.ButtonSubmit()
    </div>
    
    <div class="alert-error">
    @Html.ValidationSummary()
    </div>
    @Html.Partial("_EditorForm")

}

@section scripts
{
    <script>

        $("form").submit(function (e) {
            e.preventDefault();
            var currentForm = this;
            bootbox.confirm(function (result) {
                if (result) {
                    currentForm.submit();
                }
            });
        });

        function calculateNetAmount() {
            var excludeVat = $("#ExcludeVatAmount").val();
            var vat = $("#VatAmount").val();
            var discountPercent = $("#DiscountPercent").val();
            var extraDiscount = $("#ExtraDiscount").val();
            var netAmount = 0;
            var txtNetAmount = $("#NetAmount");

            if (excludeVat == "") {
                excludeVat = 0;
            }

            if (vat == "") {
                vat = 0;
            }

            if (discountPercent == "") {
                discountPercent = 0;
            }

            if (extraDiscount == "") {
                extraDiscount = 0;
            }

            netAmount = parseFloat(excludeVat) + parseFloat(vat);
            netAmount = netAmount - (netAmount * discountPercent / 100) - extraDiscount;
            txtNetAmount.val(netAmount);
        }

        function calculateExcludeVatAmount() {
            var price = $("#Price").val();
            var qty = $("#Qty").val();
            var txtExcludeVatAmount = $("#ExcludeVatAmount");

            if (price == "") {
                price = 0;
            }

            if (qty == "") {
                qty = 0;
            }

            txtExcludeVatAmount.val(price * qty);
            calculateNetAmount();
        }

        $(function () {
            $("#PrItemId").change(function () {
                var id = $('#PrItemId').val();
                // alert(id);
                $('#Price').empty();
                $('#Qty').empty();

                var p = 0;
                var q = 0;
                if (id != '') {
                    $.getJSON('@Url.Action("GetPrItemPrice")/' + id, function (data) {
                        $('#Price').val(data);
                    });

                    $.getJSON('@Url.Action("GetPrItemQty")/' + id, function (data) {
                        $('#Qty').val(data);
                    });

                    $.getJSON('@Url.Action("GetPrItemAmount")/' + id, function (data) {
                        $('#NetAmount').val(data);
                    });
                }

            });


            $("#Price").change(function () {
                calculateExcludeVatAmount();
            });

            $("#ExcludeVatAmount").change(function () {
                calculateNetAmount();
            });

            $("#VatAmount").change(function () {
                calculateNetAmount();
            });

            $("#DiscountPercent").change(function () {
                calculateNetAmount();
            });

            $("#ExtraDiscount").change(function () {
                calculateNetAmount();
            });
        });

    </script>
}